import re

class DataManager:
    def __init__(self,filename):
        self.file = open(filename, "a+")
        self.items=[]

    def saveItem(self, itemId,html):
        spu=""
        resultSpu = re.findall("data-spu=\"([^\"]+)\"",html)
        if(len(resultSpu)>0):
            spu = resultSpu[0]
        pid = ""
        resultPid = re.findall("data-pid=\"([^\"]+)\"",html)
        if(len(resultPid)>0):
            pid = resultPid[0]

        (title, href) = self.getTitle(itemId, html)
        (comment_href,comment_number)=self.getComment(itemId, html)
        price = self.getPrice(itemId, html)
        (shopid,shopname)= self.getShop(itemId, html)
        dic = {"商品SKU":itemId, "商品名称":title,"价格":price,
                "链接":href,"店铺编号":shopid,"店铺名":shopname,
               "评论数":comment_number,"评论链接":comment_href,"商品SPU":spu,"商品PID":pid}
        self.items.append(dic)

    def finish(self):
        for i in self.items:
            self.file.write(str(i))
            self.file.write("\n")
        self.file.close()

    def getTitle(self, itemId, str):
        p = "<a[^>]*title=\"([^\"]+)\"[^>]*href=\"([^\"]+)\"[^>]+>"
        result = re.findall(p, str)
        return result[0]

    def getComment(self,itemId, str):
        p="<strong><a id=\"J_comment_"+itemId+"\"[^>]+href=\"([^\"]+)\"[^>]+>([^<]+)</a>条评价</strong>"
        result = re.findall(p,str)
        return result[0]

    def getPrice(self,itemId,str):
        p="<strong class=\"J_"+itemId+"\"[^>]+><em>￥</em><i>([^<]+)</i></strong>"
        result = re.findall(p,str)
        if(len(result)==0):
            p = "<strong class=\"J_" + itemId + "\"[^>]+data-price=\"([^\"]+)\"[^>]+>"
            result = re.findall(p,str)

        if(len(result)==0):
            return ""
        return result[0]

    def getShop(self,itemId,str):
        p="<div class=\"p-shop\"([^>]*)>([\s\S]+?)</div>"
        result = re.findall(p,str,re.S)
        idp = "data-shopid=\"([^\"]+)\"[^>]*"
        shop = re.findall(idp,result[0][0])
        shopid = ""
        if(len(shop)>0):
            shopid = shop[0]
        shopnamestr = result[0][1]
        np = "<a[^>]+>([^<]+)</a>"
        name = re.findall(np, shopnamestr,re.S)
        shopname=""
        if(len(name)>0):
            shopname = name[0]
        return (shopid,shopname)

# 	<div class="gl-i-wrap">
# 		<div class="p-img">
# 			<a target="_blank" title="夏普 SHARP AQUOS S2 全面屏手机 全网通 4GB+64GB 晶耀黑 移动联通电信4G手机 双卡双待" href="https://ccc-x.jd.com/dsp/nc?ext=aHR0cHM6Ly9pdGVtLmpkLmNvbS80NjExNDE1Lmh0bWw&log=CZg7J1dFTXhGz7EcqMyGv1i8DtrzaEqw6Y19hCtaQrBwzMJid4nMZIwjkCiohV0tAVYfsBXvzTPOhmvvZfcHVtvT56pDRQsZhRpIzt0Ejd7fG132JoLkccS2IXgfgZ2XwyuHTDj_DagTGoM44kA-NF7Yda5A15r-7oii0ISAn7lVAoCkgy3ea0v7aVzYI48e1Q4q85wuhhK15CfBGG4Vz1TndsVUnlmE0hTzDqdBOXL1mkn6XQdLlfAj6iC3IJ-vW8TZmzWXJZGTP10-BX06HH5t9nWfRj1lIVXb1lz0WR13YPHMcmEQsxzcETUk_Kj2SCd1oIwDPr_zDam4EyPqqVeJJcjEPyzMKbsW44-MZap13bg2HZCnLZyAJPbLmDHtNeqotfMapEcclLpwGKjk657CgcnJeuH6A66k8BHeWiQ&v=404" onclick="searchlog(1,4611415,0,2,'','adwClk=1')">
# 				<img width="220" height="220" class="err-product" data-img="1" src="//img10.360buyimg.com/n7/jfs/t10546/119/1007692688/358419/49ab51a9/59db1ed4Nac9024b8.jpg" />
# </a>			<div data-catid="655" data-venid="1000076264" data-presale="0"></div>
# 		</div>
# 		<div class="p-scroll">
# 			<span class="ps-prev">&lt;</span>
# 			<span class="ps-next">&gt;</span>
# 			<div class="ps-wrap">
# 				<ul class="ps-main">
# 					<li class="ps-item"><a href="javascript:;" class="curr" title="晶耀黑"><img data-url="https://ccc-x.jd.com/dsp/nc?ext=aHR0cHM6Ly9pdGVtLmpkLmNvbS80NjExNDE1Lmh0bWw&log=CZg7J1dFTXhGz7EcqMyGv1i8DtrzaEqw6Y19hCtaQrBwzMJid4nMZIwjkCiohV0tAVYfsBXvzTPOhmvvZfcHVtvT56pDRQsZhRpIzt0Ejd7fG132JoLkccS2IXgfgZ2XwyuHTDj_DagTGoM44kA-NF7Yda5A15r-7oii0ISAn7lVAoCkgy3ea0v7aVzYI48e1Q4q85wuhhK15CfBGG4Vz1TndsVUnlmE0hTzDqdBOXL1mkn6XQdLlfAj6iC3IJ-vW8TZmzWXJZGTP10-BX06HH5t9nWfRj1lIVXb1lz0WR13YPHMcmEQsxzcETUk_Kj2SCd1oIwDPr_zDam4EyPqqVeJJcjEPyzMKbsW44-MZap13bg2HZCnLZyAJPbLmDHtNeqotfMapEcclLpwGKjk657CgcnJeuH6A66k8BHeWiQ&v=404" data-sku="4611415" data-img="1" data-lazy-img="//img10.360buyimg.com/n9/jfs/t10546/119/1007692688/358419/49ab51a9/59db1ed4Nac9024b8.jpg" class="err-product" width="25" height="25" /></a></li>
# 									</ul>
# 			</div>
# 		</div>
# 		<div class="p-price">
# <strong class="J_4611415" data-done="1"><em>￥</em><i>1699.00</i></strong>		</div>
# 		<div class="p-name p-name-type-2">
# 			<a target="_blank" title="夏普 SHARP AQUOS S2 全面屏手机 全网通 4GB+64GB 晶耀黑 移动联通电信4G手机 双卡双待" href="https://ccc-x.jd.com/dsp/nc?ext=aHR0cHM6Ly9pdGVtLmpkLmNvbS80NjExNDE1Lmh0bWw&log=CZg7J1dFTXhGz7EcqMyGv1i8DtrzaEqw6Y19hCtaQrBwzMJid4nMZIwjkCiohV0tAVYfsBXvzTPOhmvvZfcHVtvT56pDRQsZhRpIzt0Ejd7fG132JoLkccS2IXgfgZ2XwyuHTDj_DagTGoM44kA-NF7Yda5A15r-7oii0ISAn7lVAoCkgy3ea0v7aVzYI48e1Q4q85wuhhK15CfBGG4Vz1TndsVUnlmE0hTzDqdBOXL1mkn6XQdLlfAj6iC3IJ-vW8TZmzWXJZGTP10-BX06HH5t9nWfRj1lIVXb1lz0WR13YPHMcmEQsxzcETUk_Kj2SCd1oIwDPr_zDam4EyPqqVeJJcjEPyzMKbsW44-MZap13bg2HZCnLZyAJPbLmDHtNeqotfMapEcclLpwGKjk657CgcnJeuH6A66k8BHeWiQ&v=404" onclick="searchlog(1,4611415,0,1,'','adwClk=1')">
# 				<em>夏普 SHARP AQUOS S2 全面屏<font class="skcolor_ljg">手机</font> 全网通 4GB+64GB 晶耀黑 移动联通电信4G<font class="skcolor_ljg">手机</font> 双卡双待</em>
# 				<i class="promo-words" id="J_AD_4611415"></i>
# 			</a>
# 		</div>
# 		<div class="p-commit">
# 			<strong><a id="J_comment_4611415" target="_blank" href="https://ccc-x.jd.com/dsp/nc?ext=aHR0cHM6Ly9pdGVtLmpkLmNvbS80NjExNDE1Lmh0bWw&log=CZg7J1dFTXhGz7EcqMyGv1i8DtrzaEqw6Y19hCtaQrBwzMJid4nMZIwjkCiohV0tAVYfsBXvzTPOhmvvZfcHVtvT56pDRQsZhRpIzt0Ejd7fG132JoLkccS2IXgfgZ2XwyuHTDj_DagTGoM44kA-NF7Yda5A15r-7oii0ISAn7lVAoCkgy3ea0v7aVzYI48e1Q4q85wuhhK15CfBGG4Vz1TndsVUnlmE0hTzDqdBOXL1mkn6XQdLlfAj6iC3IJ-vW8TZmzWXJZGTP10-BX06HH5t9nWfRj1lIVXb1lz0WR13YPHMcmEQsxzcETUk_Kj2SCd1oIwDPr_zDam4EyPqqVeJJcjEPyzMKbsW44-MZap13bg2HZCnLZyAJPbLmDHtNeqotfMapEcclLpwGKjk657CgcnJeuH6A66k8BHeWiQ&v=404" onclick="searchlog(1,4611415,0,3,'','adwClk=1')">1.8万+</a>条评价</strong>
# 		</div>
# 		<div class="p-focus"><a class="J_focus" data-sku="4611415" href="javascript:;" title="点击关注" onclick="searchlog(1,4611415,0,5,'','adwClk=1')"><i></i>关注</a></div>
# 		<div class="p-shop" data-selfware="1" data-score="0" data-reputation="97" data-shopid="1000076264">
# 		</div>
# 		<div class="p-icons" id="J_pro_4611415">
# 			<i class="goods-icons J-picon-tips J-picon-fix" data-idx="1" data-tips="京东自营，品质保障">自营</i>
# 		</div>
# 		<span class="p-promo-flag">广告</span>
# 		<img src="https://im-x.jd.com/dsp/np?log=CZg7J1dFTXhGz7EcqMyGv1i8DtrzaEqw6Y19hCtaQrBwzMJid4nMZIwjkCiohV0tAVYfsBXvzTPOhmvvZfcHVtvT56pDRQsZhRpIzt0Ejd7fG132JoLkccS2IXgfgZ2XwyuHTDj_DagTGoM44kA-NF7Yda5A15r-7oii0ISAn7kZPqX7vQWRZusrvDjbbM4p2MtSZ_Mp8tb1y9t9fsQAccoL5cPYpeRj9qS0QGua-eDuxT2BnbrgiWhHPvSgFZazA-5Dkb0PkpytSbLdmGxBEQVygmXBLlFV_XsGBZfACAtz8jJhs7N0PpAdz5b6eqOQcj81OnuEUlm-uyrHXTPY4p8oQ0V905oII-di2TsWfYAiBsmCZBbFtxQXGrcJezWNdgGe7KWXfPxR3xl5-q9xqY6dpXVVXp9NiBPSHBBKjx4~&v=404" style="display:none;">
# 	</div>
# </li>